﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Wyn.PMS.Common.Utils;
using Wyn.PMS.Entity.DataAccess;
using Wyn.PMS.Entity.Entities;
using Wyn.PMS.Entity.IDataAccess;
using Wyn.PMS.Logic.Business;
using Wyn.PMS.Logic.IBusiness;

namespace Wyn.PMS.Test.Cache
{
    public class YuanZhiMing
    {
        private IUnityContainer container = UnityContainerService.GetContainer();

        private string _hotelId;

        private YuanZhiMing()
        {
        }

        public YuanZhiMing(string hotelId)
        {
            _hotelId = hotelId;
        }
        
        public void SetOOOTest()
        {
            bool isContinue = true;

            while(isContinue)
            {
                isContinue = false;

                Console.Write("\r\n请输入要设置的房间以及开始结束时间,例: 8090,2012-3-6,2012-3-18,\r\nInput:");

                string arg= Console.ReadLine();

                var args =  arg.Split(',');

                SetOOORoom(_hotelId,args[0], args[1], args[2]);

                Console.Write("请问是否还继续设置豁免房(Y/N)？");

                string yesOrNo = Console.ReadKey().KeyChar.ToString().ToUpper();

                if (yesOrNo.ToUpper() == "Y") isContinue = true;
            }

        }

        private  void SetOOORoom(string hotelId,string roomNo,string beginDate,string endDate)
        {
            DateTime startDate = DateTime.Now;

            DateTime.TryParse(beginDate, out startDate);

            DateTime end = DateTime.Today.AddDays(1);

            DateTime.TryParse(endDate, out end);
            
            if (string.IsNullOrEmpty(hotelId) || string.IsNullOrEmpty(roomNo))
            {
                Console.Write("请提供需要更新的房间号码。");
                return;
            }

            IRoomRepository _repositoryRoom = container.Resolve<IRoomRepository>();

            var room = _repositoryRoom.Find(hotelId, hotelId, roomNo);

            if (room == null)
            {
                Console.Write("请确认需要设置豁免的房间真的存在？");
                return;
            }

            var originStatus = room.Status;

            var destinStatus = EnumHelper.GetCode(RoomStatus.OOO);

            if (!StatusVerifyPolicy.ValidateTransferStatus(
                EnumHelper.GetValue<RoomStatus>(room.Status)
                , EnumHelper.GetValue<RoomStatus>(destinStatus)))
            {
                Console.Write("房态转换不符合规则，详情请查询操作手册");
                return;

            }

            RoomOccupy occupy = new RoomOccupy()
            {
                HotelID = hotelId,
                RoomNo = roomNo,
                RoomTypeID = room.RoomTypeID,
                OrderId = BillGenerator.CreateID(hotelId, LicenceTypeHelper.PREFIX_OCCUPY),
                Memo = "测试专用",
                OrderType = (int)OrderType.OutOfOrder,
                ModifiedTime = DateTime.Now,
                IsValid = true,
                StartDate = startDate,
                EndDate = end,
                EmployeeID = "YuanZhiMing"
            };

            room.Status = destinStatus;

            room.EmployeeID = "YuanZhiMing";

            room.OrderID = occupy.OrderId;

            IRoomControl roomControl= container.Resolve<IRoomControl>();
            
            _repositoryRoom.Update(room, true);
            //占房优先
            roomControl.OccupyRoom(occupy);
             
        }

        public void CancelOOOTest()
        {
            bool isContinue = true;

            while (isContinue)
            {
                isContinue = false;

                Console.Write("\r\n请输入要取消豁免的房间,如: 8090,8091 \r\n Input:");

                string rooms = Console.ReadLine();

                var roomNos = rooms.Split(',');

                foreach(var roomNo in roomNos)
                {
                    if(!string.IsNullOrEmpty(roomNo))
                        CancelOOORoom(_hotelId, roomNo);

                }

                Console.Write("请问是否还继续取消豁免房(Y/N)？");

                string yesOrNo = Console.ReadKey().KeyChar.ToString().ToUpper();

                if (yesOrNo.ToUpper() == "Y") isContinue = true;
            }

        }

        private void CancelOOORoom(string hotelId, string roomNo)
        {
            if (string.IsNullOrEmpty(hotelId) || string.IsNullOrEmpty(roomNo))
            {
                Console.Write("请提供需要更新的房间号码。");
                return;
            }

            IRoomRepository _repositoryRoom = container.Resolve<IRoomRepository>();

            IRoomControl roomControl = container.Resolve<IRoomControl>();

            var room = _repositoryRoom.Find(hotelId, hotelId, roomNo);

            if(room==null)
            {
                Console.Write("请确认取消的房间真的存在？");
                return;
            }

            room.EmployeeID = "YuanZhiMing";

            if (room.Status != EnumHelper.GetCode(RoomStatus.OOO))
            {
                Console.Write("请确认取消的房间是维修房。");
                return;
            }

            var originStatus = room.Status;

            var destinStatus = EnumHelper.GetCode(RoomStatus.VD);

            if (!StatusVerifyPolicy.ValidateTransferStatus(
                EnumHelper.GetValue<RoomStatus>(room.Status)
                , EnumHelper.GetValue<RoomStatus>(destinStatus)))
            {
                Console.Write("房态转换不符合规则，详情请查询操作手册");
                return;
            }

            RoomOccupy occupy = roomControl.GetRoomOccupy(hotelId, room.OrderID);

            occupy.IsValid = false;

            occupy.ModifiedTime = DateTime.Now;

            room.Status = destinStatus;

            room.OrderID = string.Empty;
            
            _repositoryRoom.Update(room, true);

            roomControl.ModifyOccupy(occupy);

        }

    }
}
